package com.gisquest.cloud.service.impl;

import com.gisquest.cloud.common.exception.BizException;
import com.gisquest.cloud.common.utils.DateUtil;
import com.gisquest.cloud.common.utils.PaginationUtils;
import com.gisquest.cloud.dao.BizLogDAO;
import com.gisquest.cloud.dao.SysLogDAO;
import com.gisquest.cloud.entity.page.Pagination;
import com.gisquest.cloud.entity.po.BizLogDbEntity;
import com.gisquest.cloud.entity.po.SysLogDbEntity;
import com.gisquest.cloud.entity.vo.RequestEntity;
import com.gisquest.cloud.entity.vo.RestResult;
import com.gisquest.cloud.entity.vo.SysLogVO;
import com.gisquest.cloud.service.DbService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;

/**
 * @Author guowei
 * @Date 2020/6/24 13:53
 * @Description:
 **/
@Service
public class DbServiceImpl implements DbService {

  @Autowired
  private BizLogDAO bizLogDAO;

  @Autowired
  private SysLogDAO sysLogDAO;

  @Override
  public RestResult<?> getBizLogFromDb(RequestEntity requestEntity) {
    PageHelper.startPage(requestEntity.getPageNum(), requestEntity.getPageSize());
    Page<BizLogDbEntity> list = (Page<BizLogDbEntity>)bizLogDAO.queryBizLogFromDb(requestEntity);
    return new RestResult<Pagination<BizLogDbEntity>>().makeSuccessResult(PaginationUtils.convertPage(list));
  }

  @Override
  public RestResult<?> getSysLogFromDb(SysLogVO sysLogVO) {
    PageHelper.startPage(sysLogVO.getPageNum(), sysLogVO.getPageSize());
    Page<SysLogDbEntity> list = (Page<SysLogDbEntity>)sysLogDAO.querySysDbLog(sysLogVO);
    return new RestResult<Pagination<SysLogDbEntity>>().makeSuccessResult(PaginationUtils.convertPage(list));
  }

  @Override
  public RestResult<?> saveBizLogToDb(BizLogDbEntity bizLogDbEntity) {
    if (StringUtils.isEmpty(bizLogDbEntity.getAppId())) {
      throw new BizException("appId不能为空");
    }
    if (StringUtils.isEmpty(bizLogDbEntity.getUserId()) || StringUtils.isEmpty(bizLogDbEntity.getUserName())) {
      throw new BizException("userId和userName均不能为空");
    }
    if (StringUtils.isEmpty(bizLogDbEntity.getYwh()) && StringUtils.isEmpty(bizLogDbEntity.getOptId())) {
      throw new BizException("业务标识和操作标识不能同时为空");
    }
    if (StringUtils.isEmpty(bizLogDbEntity.getRequestId())) {
      throw new BizException("请传入请求标识");
    }
    if (StringUtils.isEmpty(bizLogDbEntity.getRequestUrl())) {
      throw new BizException("请传入请求路径");
    }
    bizLogDbEntity.setId(UUID.randomUUID().toString().replace("-", ""));
    bizLogDbEntity.setRequestTime(DateUtil.getCurDateTimeStr());
    bizLogDAO.insert(bizLogDbEntity);
    return new RestResult<>().makeSuccessResult();
  }

  @Override
  public RestResult<?> saveSysLogToDb(SysLogVO sysLogVO) {
    if (StringUtils.isEmpty(sysLogVO.getRequestId())) {
      throw new BizException("requestId不能为空");
    }
    if (StringUtils.isEmpty(sysLogVO.getMsg())) {
      throw new BizException("日志信息不能为空");
    }
    SysLogDbEntity sysLogDbEntity = new SysLogDbEntity();
    if (StringUtils.isEmpty(sysLogVO.getCreateTime())){
      //默认为当前时间
      sysLogDbEntity.setCreateTime(DateUtil.getCurDateTimeStr());
    }else {
      sysLogDbEntity.setCreateTime(sysLogVO.getCreateTime());
    }
    sysLogDbEntity.setClassName(sysLogVO.getClassName());
    sysLogDbEntity.setLevel(sysLogVO.getLevel());
    sysLogDbEntity.setMsg(sysLogVO.getMsg());
    sysLogDbEntity.setRequestId(sysLogVO.getRequestId());
    sysLogDbEntity.setId(UUID.randomUUID().toString().replace("-", ""));
    sysLogDAO.insert(sysLogDbEntity);
    return new RestResult<>().makeSuccessResult();
  }
}
